package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.*;
import com.jy.adscm.wechat.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 报修单 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-25
 */
@Service
public class WechatRepairServiceImpl extends ServiceImpl<WechatRepairMapper, WechatRepair> implements WechatRepairService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatUserRoleMapper wechatUserRoleMapper;
    @Autowired
    ShMachineMapper shMachineMapper;
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @Autowired
    WechatSelfRepairService wechatSelfRepairService;
    @Autowired
    WechatSendRepairService wechatSendRepairService;
    @Value("${wechat.upload}")
    private  String wechatUploadPath;
    @Value("${wechat.http}")
    private  String wechatHttpPath;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertWechatRepair(WechatRepair wechatRepair, String openid) {
        synchronized (this){
            WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
            //获取最大报修单号 日期+四位流水号生成
            String orderNums="";
            String date = new SimpleDateFormat("yyMMdd").format(new Date());
            Wrapper<WechatRepair> wechatRepairWrapper=new EntityWrapper<>();
            wechatRepairWrapper.like("order_nums",date, SqlLike.RIGHT);
            wechatRepairWrapper.orderBy("order_nums",false);
            wechatRepairWrapper.last(" limit 1");
            List<WechatRepair> wechatRepairs = super.selectList(wechatRepairWrapper);
            if (wechatRepairs!=null && wechatRepairs.size()>0){
                orderNums= String.valueOf(Long.valueOf(wechatRepairs.get(0).getOrderNums())+1) ;
            }else {
                orderNums=date+"0001";
            }
            wechatRepair.setOrderNums(orderNums);
            wechatRepair.setCreator(wechatUser.getId().toString());
            wechatRepair.setPlasmaStationId(wechatUser.getPlasmaStationId().toString());
            wechatRepair.setCreateTime(new Date());
            wechatRepair.setState(0);
            boolean insert = super.insert(wechatRepair);
            return insert;
        }
    }

    @Override
    public Page<WechatRepair> selectWechatRepair(Page<WechatRepair> page, QueryWechatRepair queryWechatRepair)throws AdscmSystemException {
          //查询这个用户的基本信息
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryWechatRepair.getOpenid());
        //浆站人员就只能查询的该浆站的自己的报修单
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            return page;
        }
        Boolean  isJzUser=wechatUserRoleService.isJzUser(queryWechatRepair.getOpenid());
        Wrapper<WechatRepair> wechatRepairWrapper=new EntityWrapper<>();
        if (isJzUser){
            wechatRepairWrapper.eq("creator",wechatUser.getId());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryWechatRepair.getState(),null)){
            wechatRepairWrapper.eq("state",queryWechatRepair.getState());
        }
       page= super.selectPage(page,wechatRepairWrapper);
        List<WechatRepair> wechatRepairs = page.getRecords();
        for (WechatRepair wechatRepair : wechatRepairs) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getCreator(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatRepair.getCreator());
                wechatRepair.setCreatorName(wechatUser1);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getDealer(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatRepair.getDealer());
                wechatRepair.setDealerName(wechatUser1);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPlasmaStationId(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatRepair.getPlasmaStationId());
                wechatRepair.setPlasmaStationName(shCustomerInfo.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getMacId(),null)){
                ShMachine shMachine = shMachineMapper.selectById(wechatRepair.getMacId());
                wechatRepair.setShMachine(shMachine);
            }

            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPicture1(),null)){
                String picture1 = wechatRepair.getPicture1().replace(wechatUploadPath,wechatHttpPath);
                wechatRepair.setPicture1Info(picture1);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPicture2(),null)){
                String picture2 = wechatRepair.getPicture2().replace(wechatUploadPath,wechatHttpPath);
                wechatRepair.setPicture1Info(picture2);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPicture3(),null)){
                String picture3 = wechatRepair.getPicture3().replace(wechatUploadPath,wechatHttpPath);
                wechatRepair.setPicture1Info(picture3);
            }
        }
        page.setTotal(selectCount(wechatRepairWrapper));
        return page;
    }

    @Override
    public Boolean updateWechatRepair(WechatRepair wechatRepair, String openid) throws AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        Boolean  isJzUser=wechatUserRoleService.isJzUser(openid);
        WechatRepair oldwechatRepair = super.selectById(wechatRepair.getId());
        if (isJzUser){
          if (AdscmBaseUtils.getInstance().isNotEmpty(oldwechatRepair.getDealResult(),null)){
              throw new AdscmSystemException("不能修改,已经处理报修");
          }
        }
        if (!AdscmBaseUtils.getInstance().isNotEmpty(oldwechatRepair.getDealResult(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getDealResult(),null)){
                wechatRepair.setDealer(wechatUser.getId().toString());
                wechatRepair.setDealTime(new Date());
                //根据处理的结果生成对应的自修和报修单
                //报修类型为机器类型
                if (wechatRepair.getProblemType()==1){
                    //自修
                    if ( wechatRepair.getDealResult()==1){
                        WechatSelfRepair  wechatSelfRepair=new WechatSelfRepair();
                        wechatSelfRepair.setPid(wechatRepair.getId());
                        wechatSelfRepair.setRepairer(wechatRepair.getPerformer());
                        wechatSelfRepair.setState(0);
                        wechatSelfRepairService.insertWechatSelfRepair(wechatSelfRepair);
                    }
                    //派修
                    if (wechatRepair.getDealResult()==2){
                        WechatSendRepair  wechatSendRepair=new WechatSendRepair();
                        wechatSendRepair.setPid(wechatRepair.getId());
                        wechatSendRepair.setRepairer(wechatRepair.getPerformer());
                        wechatSendRepair.setState(0);
                        wechatSendRepairService.insertWechatSendRepair(wechatSendRepair);
                    }

                }
                wechatRepair.setState(1);
            }
        }
        boolean b = super.updateById(wechatRepair);
        return b;
    }

    @Override
    public WechatRepair selectWechatRepairById(Long id) {
        WechatRepair wechatRepair = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getCreator(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatRepair.getCreator());
            wechatRepair.setCreatorName(wechatUser1);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getDealer(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatRepair.getDealer());
            wechatRepair.setDealerName(wechatUser1);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPlasmaStationId(),null)){
            ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatRepair.getPlasmaStationId());
            wechatRepair.setPlasmaStationName(shCustomerInfo.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getMacId(),null)){
            ShMachine shMachine = shMachineMapper.selectById(wechatRepair.getMacId());
            wechatRepair.setShMachine(shMachine);
        }

        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPicture1(),null)){
            String picture1 = wechatRepair.getPicture1().replace(wechatUploadPath,wechatHttpPath);
            wechatRepair.setPicture1Info(picture1);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPicture2(),null)){
            String picture2 = wechatRepair.getPicture2().replace(wechatUploadPath,wechatHttpPath);
            wechatRepair.setPicture1Info(picture2);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getPicture3(),null)){
            String picture3 = wechatRepair.getPicture3().replace(wechatUploadPath,wechatHttpPath);
            wechatRepair.setPicture1Info(picture3);
        }

        return wechatRepair;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteWechatRepairById(Long id) throws AdscmSystemException{
        WechatRepair wechatRepair = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatRepair.getState(),null)){
            if (wechatRepair.getState()==1){
                throw new AdscmSystemException("该记录不能删除");
            }
        }
        boolean b = super.deleteById(id);
        return b;
    }
}
